package com.dhj.jvmstudy.chapter06;

import java.nio.ByteBuffer;
import java.util.ArrayList;

/**
 * OutOfMemoryError: Direct buffer memory
 * 直接内存大小可以通过MaxDirectMemorySize设置
 * 如果不指定，默认与堆的最大值-Xmx参数值一致
 */
public class BufferTest3 {

    private static final int BUFFER = 1024 * 1024 * 20; // 20M

    public static void main(String[] args) {
        ArrayList<ByteBuffer> list = new ArrayList<>();

        int count = 0;
        try {
            while (true){
                ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER);
                list.add(byteBuffer);
                count ++;
                Thread.sleep(100);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println(count);
        }
    }
}
